package com.ljkj.blockpay.presenter;

import android.content.Context;
import android.icu.text.SimpleDateFormat;

import androidx.annotation.NonNull;

import com.google.gson.JsonObject;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.JsonResponseListener;
import com.kongzue.baseokhttp.util.JsonBean;
import com.kongzue.baseokhttp.util.JsonList;
import com.kongzue.baseokhttp.util.JsonMap;
import com.kongzue.baseokhttp.util.Parameter;
import com.ljkj.blockpay.basedata.BasePresenterImpl;
import com.ljkj.blockpay.model.TimeModel;
import com.ljkj.blockpay.network.ApiToCConfig;
import com.ljkj.blockpay.presenter.inter.HistoryInter;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by yp on 2022/3/23
 */
public class HistoryPresenter extends BasePresenterImpl<HistoryInter.HistoryInterView> implements HistoryInter.HistoryInterPresenter{

    public HistoryPresenter(@NonNull Context context, @NonNull HistoryInter.HistoryInterView view) {
        super(context, view);
    }

    public int pageNum = 1;

    @Override
    public void getlist() {
        HttpRequest.GET(getContext(), ApiToCConfig.URL_LIST, new Parameter()
                .add("orderType","")
                .add("pageNum",pageNum)
                .add("pageSize",20),
                new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code")== 0){
                    JsonList jsonList = main.getList("data");
                    List list = new ArrayList();
                    for (int i = 0; i < jsonList.size(); i++) {
                        list.add(JsonBean.getBean(jsonList.getJsonMap(i),jsonList.getClass()));
                    }
                    getView().historylist(jsonList);
                }
            }
        });
    }
    public List<TimeModel> distinct(List<TimeModel> list) {
        if (null != list && list.size() > 0) {
            //循环list集合
            for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {
                for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {
                    // 这里是对象的比较，如果去重条件不一样，在这里修改即可
                    if  (list.get(j).getYearDay().equals(list.get(i).getYearDay()))  {
                        list.remove(j);
                    }
                }
            }
        }
        return list;
    }
    public String longToString(String value){
        if (value == null || value.equals("")) return "";
        long l = Long.valueOf(value);
        Date date = new Date(l);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
       return sdf.format(date);
    }


    public List<TimeModel> getTimeModeList(JsonList jsonList){
        List<TimeModel> listTime = new ArrayList<>();
        for (int i = 0; i < jsonList.size(); i++) {
            listTime.add(new TimeModel(jsonList.getJsonMap(i).getString("createTime")));
        }
        List<TimeModel> distinct = distinct(listTime);
        for (int i = 0; i < distinct.size(); i++) {
            List<TimeModel.DataTimeModel> listvalue = new ArrayList<>();
            for (int j = 0; j < jsonList.size(); j++) {
                if (distinct.get(i).getYearDay().equals(longToString(jsonList.getJsonMap(j).getString("createTime")))){
                    listvalue.add(JsonBean.getBean(jsonList.getJsonMap(j),TimeModel.DataTimeModel.class));
                }
                distinct.get(i).setList(listvalue);
            }
        }
        return listTime;
    }



}
